package org.itboys.mobile.controller.web.member;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.coo.conmon.Finals;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/12/4
 * 描述：后台_用户
 */
@Controller
@RequestMapping("/sunday/web/member")
public class SundayMemberController extends BaseController{
    @Autowired
    private SundayMemberService memberService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model){

        return "/sunday/member/index";
    }

    /**
     * 详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            Model model){

        SundayMember member = memberService.findOne(id);
        model.addAttribute("member", member);
            return "/sunday/member/input";
    }

    /**
     * 查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response) {
        try {

            PageResult<SundayMember> result=memberService.selectMember(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 获取管理链
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping("/selectManagerMember")
    public void selectManagerMember(HttpServletRequest request,
                       HttpServletResponse response,
                                    @RequestParam(value="memberId",required=true) Long memberId
                                    ) {
        try {

            PageResult<SundayMember> result=memberService.selectManagerMember(request,memberId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或者修改
     * @param request
     * @param response
     * @param member
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayMember member){
        try{
            memberService.saveMember(member);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/sunday/web/member/save/------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }
    }
    /**
     * 重置密码
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/resetPassword")
    public void resetPassword(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id){
        try {
            memberService.resetPassword(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/resetPassword------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 更换管理人
     * @param request
     * @param response
     * @param memberId
     * @param managerId
     */
    @RequestMapping("/updateManager")
    public void updateManager(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="memberId",required=true) Long memberId,
                              @RequestParam(value="managerId",required=true) Long managerId){
        try {
            memberService.updateManager(memberId,managerId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/updateManager------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }
    }

    /**
     * 删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id){
        try {
            memberService.deleteMember(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/sunday/web/member/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 用户选择器
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          @RequestParam(value="memberId",required=false)String memberId,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("memberId",memberId);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/member/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/member/choose/choose_single";
        }else if(isSingle==0){
            return "/sunday/member/choose/choose_manager";
        }

        return "/sunday/member/choose/choose_erro";
    }

}
